package org.jeecg.modules.mall.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.entity.BusHotel;
import org.jeecg.modules.business.entity.BusMarketMealCouponsUsed;
import org.jeecg.modules.business.entity.BusRoomBookingOrders;
import org.jeecg.modules.business.enums.CouponsStatusEnum;
import org.jeecg.modules.business.service.IBusHotelService;
import org.jeecg.modules.business.service.IBusRoomBookingOrdersService;
import org.jeecg.modules.mall.entity.MallHotelOrder;
import org.jeecg.modules.mall.service.IMallHotelOrderService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.rooms.entity.CesRoomLayout;
import org.jeecg.modules.rooms.service.CesRoomLayoutServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: mall_hotel_order
 * @Author: jeecg-boot
 * @Date:   2023-05-05
 * @Version: V1.0
 */
@Api(tags="mall_hotel_order")
@RestController
@RequestMapping("/mall/mallHotelOrder")
@Slf4j
public class MallHotelOrderController extends JeecgController<MallHotelOrder, IMallHotelOrderService> {
	@Resource
	private IMallHotelOrderService mallHotelOrderService;
@Resource
private IBusRoomBookingOrdersService busRoomBookingOrdersService;
	 @Resource
	 private IBusHotelService busHotelService;
	 @Resource
	 private CesRoomLayoutServiceImpl cesRoomLayoutService;
	/**
	 * 分页列表查询
	 *
	 * @param mallHotelOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "mall_hotel_order-分页列表查询")
	@ApiOperation(value="mall_hotel_order-分页列表查询", notes="mall_hotel_order-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<MallHotelOrder>> queryPageList(MallHotelOrder mallHotelOrder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<MallHotelOrder> queryWrapper = QueryGenerator.initQueryWrapper(mallHotelOrder, req.getParameterMap());
		Page<MallHotelOrder> page = new Page<MallHotelOrder>(pageNo, pageSize);
		IPage<MallHotelOrder> pageList = mallHotelOrderService.page(page, queryWrapper);
		pageList.getRecords().forEach(t -> {
			if (t.getStatus().equals(1)) {
				t.setStatusName("未付款");
			} else if (t.getStatus().equals(2)) {
				t.setStatusName("已付款");
			} else if (t.getStatus().equals(3)) {
				t.setStatusName("已取消");
			} else if (t.getStatus().equals(4)) {
				t.setStatusName("已完成");
			} else if (t.getStatus().equals(5)) {
				t.setStatusName("待入住");
			} else if (t.getStatus().equals(6)) {
				t.setStatusName("已入住");
			} else if (t.getStatus().equals(7)) {
				t.setStatusName("退款中");
			}
			BusHotel hotel = busHotelService.getById(t.getHotelId());
			if (hotel != null) {
				t.setHotelName(hotel.getName());
			}
			CesRoomLayout layout = cesRoomLayoutService.getById(t.getLayoutId());
			if (layout != null) {
				t.setLayoutName(layout.getName());
			}
		});
		return Result.OK(pageList);
	}

	 @AutoLog(value = "酒店订单确认入住")
	 @ApiOperation(value="酒店订单确认入住", notes="酒店订单确认入住")
	 @PostMapping(value = "/confirm-move")
	 public Result<String> confirmMove(@RequestBody MallHotelOrder mallHotelOrder) {
		 MallHotelOrder order = mallHotelOrderService.getById(mallHotelOrder.getId());
		 if (!order.getStatus().equals(5)) {
			 return Result.error("订单不是待入住状态");
		 }
		 order.setStatus(6);
		 mallHotelOrderService.updateById(order);

		 LambdaUpdateWrapper<BusRoomBookingOrders> updateWrapper = new UpdateWrapper().lambda();
		 updateWrapper.set(BusRoomBookingOrders::getBookingStatus,1);
		 updateWrapper.eq(BusRoomBookingOrders::getOuterOrdersNo, order.getCode());
		 busRoomBookingOrdersService.update(updateWrapper);
		 return Result.OK("确认入住成功！");
	 }

	 @AutoLog(value = "酒店订单取消")
	 @ApiOperation(value="酒店订单取消", notes="酒店订单取消")
	 @PostMapping(value = "/confirm-cancel")
	 public Result<String> confirmCancel(@RequestBody MallHotelOrder mallHotelOrder) {
		 MallHotelOrder order = mallHotelOrderService.getById(mallHotelOrder.getId());
		 if (order.getStatus().equals(4)||order.getStatus().equals(6)) {
			 return Result.error("订单已完成/已入住状态");
		 }
		 order.setStatus(3);
		 mallHotelOrderService.updateById(order);
		 return Result.OK("取消成功！");
	 }

	/**
	 *   添加
	 *
	 * @param mallHotelOrder
	 * @return
	 */
	@AutoLog(value = "mall_hotel_order-添加")
	@ApiOperation(value="mall_hotel_order-添加", notes="mall_hotel_order-添加")
	//@RequiresPermissions("mall:mall_hotel_order:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody MallHotelOrder mallHotelOrder) {
		mallHotelOrderService.save(mallHotelOrder);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param mallHotelOrder
	 * @return
	 */
	@AutoLog(value = "mall_hotel_order-编辑")
	@ApiOperation(value="mall_hotel_order-编辑", notes="mall_hotel_order-编辑")
	//@RequiresPermissions("mall:mall_hotel_order:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody MallHotelOrder mallHotelOrder) {
		mallHotelOrderService.updateById(mallHotelOrder);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "mall_hotel_order-通过id删除")
	@ApiOperation(value="mall_hotel_order-通过id删除", notes="mall_hotel_order-通过id删除")
	//@RequiresPermissions("mall:mall_hotel_order:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		mallHotelOrderService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "mall_hotel_order-批量删除")
	@ApiOperation(value="mall_hotel_order-批量删除", notes="mall_hotel_order-批量删除")
	//@RequiresPermissions("mall:mall_hotel_order:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.mallHotelOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "mall_hotel_order-通过id查询")
	@ApiOperation(value="mall_hotel_order-通过id查询", notes="mall_hotel_order-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<MallHotelOrder> queryById(@RequestParam(name="id",required=true) String id) {
		MallHotelOrder mallHotelOrder = mallHotelOrderService.getById(id);
		if(mallHotelOrder==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(mallHotelOrder);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param mallHotelOrder
    */
    //@RequiresPermissions("mall:mall_hotel_order:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, MallHotelOrder mallHotelOrder) {
        return super.exportXls(request, mallHotelOrder, MallHotelOrder.class, "mall_hotel_order");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("mall:mall_hotel_order:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, MallHotelOrder.class);
    }

}
